package com.wellsoft.mobileoa.utils.action;

import java.util.HashMap;
import java.util.Map;

import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.utils.view.DataView;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Response;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.ServiceResponse;

public class GoBackAction extends BaseAction {

	public GoBackAction(WSOA_Action action, BaseView baseView) {
		this.action = action;
		this.baseView = baseView;
	}

	@Override
	public ServiceResponse excuteAction() {
		String newUrl = null;
		boolean isReload = false;
		
		if (!SystemConfig.stringIsNullOrEmpty(action.getGoBackUrl())) {
			SystemConfig.setGoBackUrl(action.getGoBackUrl());
			SystemConfig.setGoBackTargetUrl(baseView.getUrl());
		}
		
		if(!SystemConfig.stringIsNullOrEmpty(SystemConfig.getGoBackUrl())){
			if(!SystemConfig.getGoBackUrl().equals("EMPTY")){
				newUrl = SystemConfig.getGoBackUrl();
			}else{
				SystemConfig.setGoBackUrl("");
				BaseActivity.returnGobackActivity(SystemConfig.getGoBackTargetUrl());
				return new ServiceResponse(true);
			}
		}else{
			BaseActivity tempActivity = SystemConfig.getPreviousActivity();
			if (!SystemConfig.stringIsNullOrEmpty(tempActivity.getUrl())
					&& tempActivity.getBaseView().getClass().getSimpleName()
							.equals(DataView.class.getSimpleName())) {
				newUrl = tempActivity.getUrl();
				isReload = true;
			} else {
				baseView.getBaseActivity().returnActivity();
				return new ServiceResponse(true);
			}
		}

		WSOA_HttpGet_Request request = new WSOA_HttpGet_Request();
		request.setUrl(newUrl);
		request.setCookies(SystemConfig.getCookies());

		Map<String, BaseObject> httpRequests = new HashMap<String, BaseObject>();

		httpRequests.put("request", request);
		ServiceResponse httpGetResponse = SystemConfig.getService()
				.Callservice(httpRequests, "WSOA_HttpGet",
						WSOA_HttpGet_Response.class);
		if (!httpGetResponse.getExeFlag()) {
			return httpGetResponse;
		}
		WSOA_HttpGet_Response response = (WSOA_HttpGet_Response) httpGetResponse
				.getResponseData();
		if (response.getExeFlag() == 0) {
//			baseView.getBaseActivity().showMessage(response.getMessage());
			return new ServiceResponse(false, response.getMessage());
		}
		if (response.getExeFlag() == 2) {
			if (SystemConfig.reLogin()) {
				return excuteAction();
			} else {
				BaseActivity.returnLoginActivity();
				return new ServiceResponse(false);
			}
		}
		SystemConfig.setGoBackUrl("");
		if (!isReload) {
			BaseActivity.returnGobackActivity(
					SystemConfig.getGoBackTargetUrl(), response.getNextPage(),
					newUrl);
		} else {
			baseView.getBaseActivity().returnActivity();
			baseView.setBaseActivity(SystemConfig.getCurrentActivity());
			baseView.getBaseActivity()
					.reloadView(response.getNextPage(), newUrl);
		}
		return new ServiceResponse(true);
	}

}
